package edu.chl.da.ejb.persistency;

import edu.chl.da.ejb.core.Airplane;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaQuery;

/**
 *
 * @author Jakob
 */
@Stateless
public class AirplaneFacade implements IAirplaneFacade {
    @PersistenceContext(unitName = "airline_pu")
    private EntityManager em;

    @Override
    public Airplane create(Airplane airplane) {
        em.persist(airplane);
        return airplane;
    }

    @Override
    public void edit(Airplane airplane) {
        em.merge(airplane);
    }

    @Override
    public void remove(Airplane airplane) {
        em.remove(em.merge(airplane));
    }

    @Override
    public Airplane find(Object id) {
        return em.find(Airplane.class, id);
    }

    @Override
    public List<Airplane> findAll() {
        CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
        cq.select(cq.from(Airplane.class));
        return em.createQuery(cq).getResultList();
    }

}
